package com.lab.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lab.pojo.Oexperiment;
import com.lab.service.OexService;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/oex")
public class OexController {
    @Resource
    OexService oexService;
    private Map<String,Object> result= new HashMap<>();
    /**
     * 查询所有教学实验信息
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/listByPage")
    public Map<String,Object> listByPage(Integer page, Integer rows){//,HttpSession session
        //String id= (String) session.getAttribute("Id");
        //String role= (String) session.getAttribute("role");

        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
//        if (role.equals("教师")){
//
//        }else if(role.equals("学生")){
//
//        }else{
//
//        }
        List<Oexperiment> list= oexService.allOexperiments();
        //使用PageInfo封装查询结果
        PageInfo<Oexperiment> pageInfo=new PageInfo<Oexperiment>(list);
        try{
            for(Oexperiment oexperiment:list){
                if(oexperiment.getLaState().equals("0")){
                    oexperiment.setLaState("未通过");
                }else if (oexperiment.getLaState().equals("1")){
                    oexperiment.setLaState("通过");
                }else{
                    oexperiment.setLaState("待审核");
                }
                if(oexperiment.getTaState().equals("0")){
                    oexperiment.setTaState("未通过");
                }else if (oexperiment.getTaState().equals("1")){
                    oexperiment.setTaState("通过");
                }else{
                    oexperiment.setTaState("待审核");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Oexperiment> oList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", oList);
        return result;
    }

    /**
     * 添加和修改
     * @param oexperiment
     * @return
     */
    @PostMapping("/saveOexperiment")
    public Map<String,Object> saveOexperiment(Oexperiment oexperiment){
        try {
            oexService.saveOexperiment(oexperiment);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 数据回显
     * @param oeId
     * @return
     */
    @RequestMapping(value="/findOexById",method = RequestMethod.GET)
    public Oexperiment findOexById(Long oeId) {
        System.out.println(oexService.findOexById(oeId));
        Oexperiment o=oexService.findOexById(oeId);
        System.out.println("-------------------------------");
        System.out.println(o.getOrderedLab());
        return oexService.findOexById(oeId);
    }
    /**
     *删除数据
     * @param oeId
     * @return
     */
    @RequestMapping(value="/deleteOexperiment",method = RequestMethod.DELETE)
    public Map<String,Object> deleteOexperiment(Long[] oeId){
        try {
            oexService.deleteOexperiment(oeId);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
    /**
     * 开放性实验预约
     * @param oexperiment
     * @return
     */
    @PostMapping("/appointmentOex")
    public Map<String,Object> appointmentOex(Oexperiment oexperiment){
        try {
            oexService.ordOexperiment(oexperiment);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 审核通过
     * @param oeId
     * @return
     */
    @RequestMapping(value="/passOexperiment")
    public Map<String,Object> passOexperiment(Long[] oeId,String role){
        try {
            if(role.equals("实验室管理员")){
                oexService.passOexByLabmanager(oeId);
                result.put("success", true);
            }else if(role.equals("实验室管理员")){
                oexService.passOexByTeacher(oeId);
                result.put("success", true);
            }else{
                result.put("success", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 审核不通过
     * @param oeId
     * @return
     */
    @RequestMapping(value="/unpassOexperiment")
    public Map<String,Object> unpassOexperiment(Long[] oeId,String role){
        try {
            if (role.equals("实验室管理员")){
                oexService.unpassOexByLabmanager(oeId);
                result.put("success", true);
            }
            if (role.equals("教师")){
                oexService.unpassOexByTeacher(oeId);
                result.put("success", true);
            }
            result.put("success", false);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
}
